package com.founder.android.util;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;

/**
 * <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
 * @author chen_weihua
 *
 */
public class GPSHelper {
	
	private Context context;
	
	private LocationManager locationManager;
	
	private String provider;
	
	public GPSHelper(Context context) {
		this.context = context;
	}
	
	public boolean openGPS() {
		locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
		if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
			return true;
		}
		return false;
		//Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
		//context.startActivity(intent);
	}
	
	public Location getLocationOnce() {
        Location location = locationManager.getLastKnownLocation(getProvider()); // 通过GPS获取位置
        return location;
	}
	
	public void setLocationListener(LocationListener listener) {
		// 设置监听器，自动更新的最小时间为间隔N秒(1秒为1*1000，这样写主要为了方便)或最小位移变化超过N米
        locationManager.requestLocationUpdates(getProvider(), 100 * 1000, 500, listener);
	}
	
	private String getProvider() {
		if (provider != null) {
			return provider;
		}
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗

        provider = locationManager.getBestProvider(criteria, true); // 获取GPS信息
        return provider;
	}

}
